package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IMedcineCompanyDAO;
import com.trkj.dao.jpa.IMedcineDetailDAO;
import com.trkj.dao.jpa.IMedcineType;
import com.trkj.dao.jpa.IStorageHasDrugsDAO;
import com.trkj.dao.mybatis.MedcineDetailMapper;
import com.trkj.pojos.MedcineCompany;
import com.trkj.pojos.MedcineDetail;
import com.trkj.pojos.MedcineType;
import com.trkj.vo.MedcineDetailVo;

/**
 * 药品类别
 * 
 * @author Administrator
 *
 */
@Service
@Transactional
public class MedcineTypeService {
	@Autowired
	private IMedcineType medcineTypeDao;
	@Autowired
	private IMedcineCompanyDAO companyDAO;
	@Autowired
	private IMedcineDetailDAO detailDAO;
	@Autowired
	private MedcineDetailMapper detailMapper;
	@Autowired
	private IStorageHasDrugsDAO storageHasDrugsDAO;
	/**
	 * 根据药品名查询药库库存
	 * @param medcineName
	 * @return
	 */
	public BigDecimal findOneNumber(String medcineName) {
		return storageHasDrugsDAO.findOneDrugsNumber(medcineName);
	}
	/**
	 * 删除一条药品详情
	 * @param medcineId
	 */
	public void deleteMedcineDetail(Long medcineId) {
		detailDAO.delete(medcineId);
	}
	
	/**
	 * 新增药品单位
	 * @param medcineCompanyName
	 */
	public void addMedcineCompany(String medcineCompanyName) {
		MedcineCompany company = new MedcineCompany(medcineCompanyName);
		companyDAO.save(company);
	}
	
	/**
	 * 按条件查询药品详情
	 * @param map
	 */
	public Map<String, Object> searchSomeMedcineDetail(int page,int rows,Map map) {
		Page<MedcineDetail> p =PageHelper.startPage(page, rows,true);
		List<MedcineDetail> details = detailMapper.findBySome(map);
		//对返回的数据进行筛选
		List<Map<String,Object>> newDetails = new ArrayList<>();
		//要将details转成string
		if(!details.isEmpty()) {
			String a= JSON.toJSONString(details);
			System.out.println(a);
			JSONArray arr = JSONArray.parseArray(a);
			for(int i = 0;i<arr.size();i++) {
				Map<String,Object> map2 = new HashMap<>();
				JSONObject obj = arr.getJSONObject(i);
				JSONObject medcineCompany = obj.getJSONObject("medcineCompany");
				JSONObject medcineType = obj.getJSONObject("medcineType");
				map2.put("medcineBaozhiqi", obj.getDouble("medcineBaozhiqi"));
				map2.put("medcineEffect", obj.getString("medcineEffect"));
				map2.put("medcineId", obj.getInteger("medcineId"));
				map2.put("medcineImage", obj.getString("medcineImage"));
				map2.put("medcineName", obj.getString("medcineName"));
				map2.put("medcineNote", obj.getString("medcineNote"));
				map2.put("medcinePrice", obj.getString("medcinePrice"));
				map2.put("medcineTypeName", medcineType.getString("medcineTypeName"));
				map2.put("medcineCompanyName", medcineCompany.getString("medcinecompanyName"));
				newDetails.add(map2);
			}
		}
		System.out.println("---"+newDetails);
		Map<String,Object> maps= new HashMap<>();
		maps.put("total", details.size());
		maps.put("rows", newDetails);
		return maps;
	}
	
	/**
	 * 修改药品详情
	 * @param medcineName
	 * @param medcineType
	 * @param medcinePrice
	 * @param medcineCompany
	 * @param medcineBaoZhiQi
	 * @param medcineNote
	 * @param medcineEffect
	 * @param medcineImage
	 */
	public void editMedcineDetail(Integer medcineId,String medcineName, Integer medcineType, Double medcinePrice, Integer medcineCompany,Integer medcineBaoZhiQi,String medcineNote,String medcineEffect,String medcineImage) {
		try {
			if(medcineId!=null) {
				MedcineDetail detail = detailDAO.findOne(medcineId.longValue());
				if(medcineCompany!=null&&medcineType!=null) {
					detail.setMedcineName(medcineName);
					detail.setMedcinePrice(BigDecimal.valueOf(medcinePrice));
					detail.setMedcineBaozhiqi(BigDecimal.valueOf(medcineBaoZhiQi));
					detail.setMedcineEffect(medcineEffect);
					detail.setMedcineNote(medcineNote);
					detail.setMedcineImage(medcineImage);
					detail.setMedcineType(medcineTypeDao.findOne(medcineType.longValue()));
					detail.setMedcineCompany(companyDAO.findOne(medcineCompany.longValue()));
				}else if(medcineCompany==null&&medcineType!=null) {
					detail.setMedcineName(medcineName);
					detail.setMedcinePrice(BigDecimal.valueOf(medcinePrice));
					detail.setMedcineBaozhiqi(BigDecimal.valueOf(medcineBaoZhiQi));
					detail.setMedcineEffect(medcineEffect);
					detail.setMedcineNote(medcineNote);
					detail.setMedcineImage(medcineImage);
					detail.setMedcineType(medcineTypeDao.findOne(medcineType.longValue()));
					//detail.setMedcineCompany(companyDAO.findOne(medcineCompany.longValue()));
				}else if(medcineCompany!=null&&medcineType==null) {
					detail.setMedcineName(medcineName);
					detail.setMedcinePrice(BigDecimal.valueOf(medcinePrice));
					detail.setMedcineBaozhiqi(BigDecimal.valueOf(medcineBaoZhiQi));
					detail.setMedcineEffect(medcineEffect);
					detail.setMedcineNote(medcineNote);
					detail.setMedcineImage(medcineImage);
					//detail.setMedcineType(medcineTypeDao.findOne(medcineType.longValue()));
					detail.setMedcineCompany(companyDAO.findOne(medcineCompany.longValue()));
				}else {
					detail.setMedcineName(medcineName);
					detail.setMedcinePrice(BigDecimal.valueOf(medcinePrice));
					detail.setMedcineBaozhiqi(BigDecimal.valueOf(medcineBaoZhiQi));
					detail.setMedcineEffect(medcineEffect);
					detail.setMedcineNote(medcineNote);
					detail.setMedcineImage(medcineImage);
				}
			}else {
				System.out.println("medcineId为空");
			}
		}catch(Exception e) {
			System.out.println("再修改一个药品详情时出错");
		}
	}
	/**
	 * 查找一个药品单位类
	 * @param medcineCompanyId
	 * @return
	 */
	public MedcineCompany getMedcineCompanyById(Long medcineCompanyId) {
		return companyDAO.findOne(medcineCompanyId);
	}
	
	/**
	 * 查找一个药品类型类
	 * @param medcineTypeId
	 * @return
	 */
	public MedcineType getMedcineTypeById(Long medcineTypeId) {
		return medcineTypeDao.findOne(medcineTypeId);
	}
	
	/**
	 * 添加一个新的药品详情
	 * @param detail
	 */
	public void addMedcineDetail(MedcineDetail detail) {
		detailDAO.save(detail);
	}
	/**
	 * 查找所有药品类
	 * 
	 * @return
	 */
	public List<MedcineType> getAllMedcineType() {
		return medcineTypeDao.findAllMedcineType();
	}

	/**
	 * 查询所有的药品单位
	 * 
	 * @return
	 */
	public List<MedcineCompany> getAllMedcineCompany() {
		return companyDAO.getAllMedcineCompany();
	}
	
	/**
	 * 分页查询药品类
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getAllmedcineTypeMap(int page,int rows) {
		int n = (page-1)*rows+1;
		int total = medcineTypeDao.findAllMedcineType().size();
		List<MedcineType> medcineTypes = new ArrayList<>();
		if(rows<=total-n+1) {
			medcineTypes.addAll(medcineTypeDao.findAllMedcineType().subList(n-1, n+rows-1));
		}else {
			medcineTypes.addAll(medcineTypeDao.findAllMedcineType().subList(n-1, total));
		}
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", medcineTypes);
		return map;
	}
	
	/**
	 * 分页查询药品单位
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getAllmedcineCommpanyMap(int page,int rows) {
		int n = (page-1)*rows+1;
		int total = companyDAO.getAllMedcineCompany().size();
		List<MedcineCompany> companies= new ArrayList<>();
		if(rows<=total-n+1) {
			companies.addAll(companyDAO.getAllMedcineCompany().subList(n-1, n+rows-1));
		}else {
			companies.addAll(companyDAO.getAllMedcineCompany().subList(n-1, total));
		}
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", companies);
		return map;
	}
	
	/**
	 * 分页查询所有的药品详情
	 * 
	 * @return
	 */
	public Map getAllMedcineDetail(int page, int rows) {
		int n = (page - 1) * rows +1;
		int total = detailDAO.findAllMedcineDetail().size();
		List<MedcineDetailVo> detailVos = new ArrayList<>();
		if (rows <= total - n + 1) {
			detailVos.addAll(detailDAO.findAllMedcineDetail().subList(n-1, n + rows-1));
		} else {
			detailVos.addAll(detailDAO.findAllMedcineDetail().subList(n-1, total));

		}
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", detailVos);
		return map;
	}
	
	/**
	 * 新增药品类
	 * @param medcineTypeName
	 * @param medcineTypeEffect
	 * @param medcineTypeNote
	 */
	public void addMedcineType(String medcineTypeName,String medcineTypeEffect,String medcineTypeNote) {
		MedcineType medcineType = new MedcineType(medcineTypeEffect, medcineTypeName, medcineTypeNote);
		medcineTypeDao.save(medcineType);	
	}
	
	/**
	 * 删除药品类
	 * @param medcinetypeId
	 */
	public void deleteMedcineType(Long medcinetypeId) {
		medcineTypeDao.delete(medcinetypeId);
	}
}
